package com.homelock.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.homelock.entity.HlFamilyLinkage;
import com.homelock.entity.HlFamilyLinkageExample;
import com.homelock.mapper.HlFamilyLinkageMapper;
import com.pi.base.enumerate.record.RecordState;
import com.pi.base.exception.ServiceException;

@Service
public class HlFamilyLinkageService {

    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(HlFamilyLinkageService.class);

    private HlFamilyLinkageMapper familyLinkageMapper;

    @Autowired
    public HlFamilyLinkageService(HlFamilyLinkageMapper familyLinkageMapper) {
        Assert.notNull(familyLinkageMapper, "锁具亲情服务为空");
        this.familyLinkageMapper = familyLinkageMapper;
    }

    public Long add(Long devId, Long userId, Long targetUserId) {
        HlFamilyLinkage familyLinkageInfo = getFamilyLinkageInfoIfExist(devId, userId, targetUserId);
        if (null != familyLinkageInfo && familyLinkageInfo.getState()) {
            throw new ServiceException("亲情关联已存在");
        }
        HlFamilyLinkage record = new HlFamilyLinkage();
        record.setDevId(devId);
        record.setUserId(userId);
        record.setTargetUserId(targetUserId);
        record.setCreateDate(new Date());
        record.setUpdateDate(record.getCreateDate());
        record.setState(RecordState.STATE_NORMAL.getState());
        if (null != familyLinkageInfo) {
            record.setId(familyLinkageInfo.getId());
            familyLinkageMapper.updateByPrimaryKeySelective(record);
        } else {
            familyLinkageMapper.insertSelective(record);
        }
        return record.getId();
    }

    public void removeone(Long devId, Long userId, Long targetUserId) {
        HlFamilyLinkage familyLinkageInfo = getFamilyLinkageInfo(devId, userId, targetUserId);
        HlFamilyLinkage update = new HlFamilyLinkage();
        update.setId(familyLinkageInfo.getId());
        update.setState(RecordState.STATE_DELETE.getState());
        familyLinkageMapper.updateByPrimaryKeySelective(update);
    }

    public void removeAll(Long devId) {
        HlFamilyLinkageExample example = new HlFamilyLinkageExample();
        example.createCriteria().andDevIdEqualTo(devId);
        List<HlFamilyLinkage> familyLinkages = familyLinkageMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(familyLinkages)) {
            return;
        }
        List<Long> ids = new ArrayList<>(100);
        int size = familyLinkages.size();
        for (HlFamilyLinkage familyLinkage : familyLinkages) {
            ids.add(familyLinkage.getId());
            if (ids.size() >= 100 || size == 1) {
                HlFamilyLinkage record = new HlFamilyLinkage();
                record.setState(RecordState.STATE_DELETE.getState());
                example.clear();
                example.createCriteria().andIdIn(ids);
                familyLinkageMapper.updateByExampleSelective(record, example);
                ids.clear();
            }
            size--;
        }
    }

    public void updateUserUnlockName(Long devId, Long userId, Long targetUserId) {
        HlFamilyLinkage familyLinkageInfo = getFamilyLinkageInfo(devId, userId, targetUserId);
        HlFamilyLinkage updated = new HlFamilyLinkage();
        updated.setDevId(devId);
        updated.setUserId(userId);
        updated.setTargetUserId(targetUserId);
        updated.setId(familyLinkageInfo.getId());
        updated.setVersion(familyLinkageInfo.getVersion() + 1);
        updated.setUpdateDate(new Date());
        familyLinkageMapper.updateByPrimaryKeySelective(updated);
    }

    public HlFamilyLinkage getFamilyLinkageInfo(Long devId, Long userId, Long targetUserId) {
        HlFamilyLinkage familyLinkageInfo = getFamilyLinkageInfoIfExist(devId, userId, targetUserId);
        if (null == familyLinkageInfo || !familyLinkageInfo.getState()) {
            throw new ServiceException("亲情关联不存在");
        }
        return familyLinkageInfo;
    }

    public HlFamilyLinkage getFamilyLinkageInfoIfExist(Long devId, Long userId, Long targetUserId) {
        HlFamilyLinkageExample example = new HlFamilyLinkageExample();
        example.createCriteria().andDevIdEqualTo(devId).andUserIdEqualTo(userId).andTargetUserIdEqualTo(targetUserId);
        List<HlFamilyLinkage> devUserist = familyLinkageMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(devUserist)) {
            return null;
        }
        return devUserist.get(0);
    }

    public List<HlFamilyLinkage> getallInfo(Long devId) {
        HlFamilyLinkageExample example = new HlFamilyLinkageExample();
        example.createCriteria().andDevIdEqualTo(devId).andStateEqualTo(RecordState.STATE_NORMAL.getState());
        return familyLinkageMapper.selectByExample(example);
    }

    public void clearByDevId(Long devId) {
        HlFamilyLinkageExample example = new HlFamilyLinkageExample();
        example.createCriteria().andDevIdEqualTo(devId).andStateEqualTo(RecordState.STATE_NORMAL.getState());
        HlFamilyLinkage record = new HlFamilyLinkage();
        record.setState(RecordState.STATE_DELETE.getState());
        familyLinkageMapper.updateByExampleSelective(record, example);
    }

    /**
     * 查询某设备某用户开门的时候的亲情关联
     * @param devId
     * @param userId
     * @return
     */
    public List<HlFamilyLinkage> queryTargetUserByDevIdAndUserId(Long devId,Long userId){
        HlFamilyLinkageExample example = new HlFamilyLinkageExample();
        example.createCriteria().andDevIdEqualTo(devId)
                .andUserIdEqualTo(userId)
                .andStateEqualTo(RecordState.STATE_NORMAL.getState());
        return familyLinkageMapper.selectByExample(example);
    }
}
